#include "stm32f10x.h"                  // Device header
#include "Timer.h"
uint16_t num;

extern float Angle;
extern float Angle1;


void Timer_Init(void)
{
	 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
		NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	TIM_InternalClockConfig(TIM3);
	
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1   ;
	TIM_TimeBaseInitStruct.TIM_CounterMode =TIM_CounterMode_Up  ;
	TIM_TimeBaseInitStruct.TIM_Period = 100-1 ;
	TIM_TimeBaseInitStruct.TIM_Prescaler= 7200-1 ;
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=  0;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	
	TIM_ClearFlag(TIM3,TIM_FLAG_Update);
	
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	

	NVIC_InitStruct.NVIC_IRQChannel =  TIM3_IRQn ;
	NVIC_InitStruct.NVIC_IRQChannelCmd=  ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority =1 ;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStruct);
	
	
	TIM_Cmd(TIM3,ENABLE);
	
}


uint16_t Timer_Get(void)
{
	return  num;
	
}

void TIM3_IRQHandler (void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
	{
		num++;
		if(num>100)
		{
			num=0;
		}
		if(0<num&&num<=25)
		{
			Angle1=Angle1-15;
		}
		if(1<num&&num<=2)
		{
			Angle=Angle+12;
			
		}
		if(2<num&&num<=3)
		{
			Angle1=Angle1+15;
			
		}
		if(3<num&&num<=4)
		{
			Angle=Angle-12;
			
		}
		
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
	}
	
}
